//
//  OMCreateOrderViewController.m
//  ERP
//
//  Created by QA on 15/9/21.
//  Copyright (c) 2015年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "CRECOMCreateOrderViewController.h"
#import "GEInputTableViewCell.h"

#import "NSDictionary+DataFormat.h"
#import "GETimeDifference.h"
#import "GEDateFromatter.h"

#import "GEPickerTextField.h"
#import "NSMutableDictionary+Format.h"
#import "GEDateTextField.h"

#import "OMOrderRealtimeDataViewController.h"
#import "CRECOMOrderConfirmationViewController.h"
#import "OMOrderProjectDataViewController.h"
#import "CRECOMOrderProjectSiteController.h"
#import "NSKeyedArchiverUserLogin.h"

@interface CRECOMCreateOrderViewController ()<BackResultDataDelagate>

@property (nonatomic, strong) NSDictionary *allContractInfo;
@property (nonatomic, strong) NSArray *projectArray;
@property (nonatomic, strong) NSDictionary *projectInfo;
@property (nonatomic, strong) NSArray *projectPosition;
@property (nonatomic, assign) NSInteger orderID;

@end

@implementation CRECOMCreateOrderViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"新增浇筑令";
        UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:@selector(sendButtonEvent:)];
        self.navigationItem.rightBarButtonItem = leftButton;
    }
    return self;
}

//修改订单
- (id)initWithModifyValue:(id)object
{
    self = [super init];
    if (self) {
        self.value = object;
        self.title = @"修改浇筑令";
        
        NSDictionary *mDictionary = object;
        self.orderID = [mDictionary objectFormatIntegerForKey:@"ID"];
    }
    return self;
}

- (void)getPassingParameter:(id)object
{
    if(!object) return;
    if(![object isKindOfClass:[NSDictionary class]]) return;
    
    //网络获取合同信息
    [self getContractInfoToNetwork:object];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 10)];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 10)];
    
    //提前加载预绘制项目
    NSInteger section = [self numberOfSectionsInTableView:_tableView];
    for (int i=0; i<section; i++) {
        NSInteger row = [self tableView:_tableView numberOfRowsInSection:i];
        for(int j=0; j<row; j++){
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:j inSection:i];
            [self tableView:_tableView cellForRowAtIndexPath:indexPath];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)sendButtonEvent:(UIButton *)button
{
    //获取当前选择时间与计划开盘时间比较
    UITextField *textFieldTime = [self.tableView getCellSaveClass:[UITextField class] pathForRow:5 inSection:0];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *inputDate = [dateFormat dateFromString:textFieldTime.text];
    
    UITextField *textField10 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:0 inSection:1];
    UITextField *textField11 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:1];
    UITextField *textField02 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:2 inSection:0];
    
    UITextField *textField20 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:0 inSection:2];
    UITextField *textField05 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:3 inSection:2];
    //UITextField *textField24 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:4 inSection:2];
    
    //合同剩余量 1否，2是
    CGFloat SYDDL = [self.allContractInfo objectFormatFloatForKey:@"剩余签订量"];
    NSNumber *SFFLKZ_Number = [self.allContractInfo objectFormatIntegerNumberForKey:@"FLKZ"];
    BOOL SFFLKZ = [SFFLKZ_Number integerValue] == 2 ? YES : NO;
    
    //计划方量
    CGFloat SRFL = [textField20.text floatValue];
    GEPickerTextField *textField06 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:6 inSection:0];
    GEPickerTextField *textField07 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:2];
    
    //获取当前时间
    NSDate *today = [GEDateFromatter getNowDateFromatAnDate:[NSDate date]];
    BOOL isTimeRight = [GETimeDifference compareFirstDatae:inputDate secondDate:today];
    
    if (!self.allContractInfo) {
        [self showAlertView:@"请选择合同编号"];
        
    } else if(!self.projectInfo) {
        [self showAlertView:@"请选择工程名称"];
        
    } else if(textField02.text.length <= 0) {
        [self showAlertView:@"请输入施工班组"];
        
    } else if(textFieldTime.text.length <= 0) {
        [self showAlertView:@"请选择开盘时间"];
        
    } else if(!isTimeRight) {
        [self showAlertView:@"开盘时间必须大于当前时间"];
        
    } else if(!(textField06.selectData && textField06.selectData.count>0)) {
        [self showAlertView:@"请选择分发站点"];
        
    } else if(textField10.text.length <= 0) {
        [self showAlertView:@"请输入工程部位"];
        
    } else if(textField11.text.length <= 0) {
        [self showAlertView:@"请输入施工部位"];
        
    } else if(textField20.text.length <= 0) {
        [self showAlertView:@"请输入计划方量"];
        
    } else if(SRFL <= 0 && SFFLKZ) {
        [self showAlertView:@"您输入的计划方量必须大于1方"];
        
    } else if(SRFL > SYDDL && SFFLKZ) {
        NSString *value = [NSString stringWithFormat:@"您输入的计划方量已超出合同剩余量%0.1f方，请重新输入！",SRFL - SYDDL];
        [self showAlertView:value];
        
    } else if(!(textField07.selectData && textField07.selectData.count>0)) {
        [self showAlertView:@"请选择强度等级"];
        
    } else if(textField05.text.length <= 0) {
        [self showAlertView:@"请选择浇筑方式"];
        
    } else if([self.allContractInfo objectFormatIntegerForKey:@"SFDDKY"] <= 0){
        NSString *errorInfo = [self.allContractInfo objectFormatStringForKey:@"报废备注" defaults:@"无"];
        NSString *alertString = [NSString stringWithFormat:@"您选择的合同%@，请选择其他合同",errorInfo];
        [self showAlertView:alertString];
        
    } else {
        [self orderGenerateHandle];
    }
}

- (void)orderGenerateHandle
{
    GEPickerTextField *textField07 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:2];
    GEPickerTextField *textField09 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:3 inSection:2];
    GEPickerTextField *textField05 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:6 inSection:0];
    
    UITextField *textField02s = [self.tableView getCellSaveClass:[UITextField class] pathForRow:3 inSection:0];
    UITextField *textField02 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:4 inSection:0];
    UITextField *textField03 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:2 inSection:0];
    UITextField *textField04 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:5 inSection:0];
    
    //UITextField *textField10 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:0 inSection:1];
    UITextField *textField11 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:1];
    
    UITextField *textField06 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:0 inSection:2];
    UITextField *textField14 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:4 inSection:2];
    
    UITextField *textField20 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:0 inSection:3];
    UITextField *textField21 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:3];
    UITextField *textField22 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:2 inSection:3];
    
    //坍落度信息
    GEInputTableViewCell *TLDCell = (GEInputTableViewCell*)[self.tableView getCellViewInRow:2 inSection:2];
    NSString *TLDValue = [TLDCell getMultiTextFieldValue];
    BOOL isInputValue = [TLDCell isMultiTextFieldInputValue];
    if(!isInputValue) TLDValue = @"";
    
    NSMutableDictionary *mDictionary = [NSMutableDictionary dictionary];
    [mDictionary setFormatObject:@(_orderID) forKey:@"订单ID"];
    [mDictionary setFormatObject:self.allContractInfo forKey:@"合同编号"];
    [mDictionary setFormatObject:self.projectInfo forKey:@"合同工程"];
    [mDictionary setFormatObject:textField03.text forKey:@"施工班组"];
    [mDictionary setFormatObject:textField02s.text forKey:@"工地地址"];
    [mDictionary setFormatObject:textField02.text forKey:@"原材要求"];
    [mDictionary setFormatObject:textField04.text forKey:@"开盘时间"];
    [mDictionary setFormatObject:textField05.selectData forKey:@"分发站点"];
    
    [mDictionary setFormatObject:self.projectPosition[0] forKey:@"工程部位ID"];
    [mDictionary setFormatObject:self.projectPosition[1] forKey:@"工程部位名称"];
    [mDictionary setFormatObject:textField11.text forKey:@"施工部位"];
    
    [mDictionary setFormatObject:textField06.text forKey:@"计划方量"];
    [mDictionary setFormatObject:textField07.selectData forKey:@"强度等级"];
    [mDictionary setFormatObject:TLDValue forKey:@"坍落度"];
    [mDictionary setFormatObject:textField09.selectData forKey:@"浇筑方式"];
    [mDictionary setFormatObject:textField14.text forKey:@"运输距离"];
    
    [mDictionary setFormatObject:textField20.text forKey:@"工地联系人"];
    [mDictionary setFormatObject:textField21.text forKey:@"工地联系电话"];
    [mDictionary setFormatObject:textField22.text forKey:@"备注"];
    
    CRECOMOrderConfirmationViewController *mViewController = [[CRECOMOrderConfirmationViewController alloc] initWithValue:mDictionary];
    [self.navigationController pushViewController:mViewController animated:YES];
}

#pragma mark
#pragma mark - 获取合同信息（复制下单、修改订单才会调用）

- (void)getContractInfoToNetwork:(NSDictionary*)mDictionary
{
    NSString *contractID = [mDictionary objectFormatStringForKey:@"合同ID"];
    
    //@状态（全部、供毕、正供、待供）
    WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
    [content setShowLoadingPrompt:self.navigationController labelText:@"正在复制中"];
    [content setInterfaceType:InterfaceTypeGroup];
    [content setModuleType:@"HTGL_HTLB_LIST_20170117"];
    [content setResultBackType:ResultBackTypeCommonUnordered];
    
    [content addParameterKey:@"起始条数" value:@0];
    [content addParameterKey:@"结束条数" value:@10];
    [content addParameterKey:@"合同性质" value:@""];
    [content addParameterKey:@"审核状态" value:@""];
    [content addParameterKey:@"合同状态" value:@""];
    [content addParameterKey:@"分发状态" value:@""];
    [content addParameterKey:@"客户编号" value:@""];
    [content addParameterKey:@"检索条件" value:@""];
    [content addParameterKey:@"合同ID" value:contractID];
    
    UserLoginNSCoding *userInfo = [NSKeyedArchiverUserLogin getUserLoginData];
    [content addParameterKey:@"用户ID" value:userInfo.Id];
    
    [content startAsyncConnectCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        //添加数据，刷新页面
        NSArray *firstArray = nil;
        if(result.resultArray && result.resultArray.count>0)
            firstArray = [result.resultArray firstObject];
        
        if(firstArray && firstArray.count>0){
            [self orderViewShowProcessing:mDictionary result:firstArray];
        } else{
            [self showErrorHUDText:@"复制失败"];
        }
    } failed:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        [self showErrorHUDText:@"复制失败"];
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        [self showErrorHUDText:@"复制失败"];
    }];
}

- (void)orderViewShowProcessing:(NSDictionary*)copyOrderInfo result:(NSArray*)result
{
    //设置合同信息到订单
    self.allContractInfo = [result firstObject];
    [self setContractInfo:_allContractInfo];
    
    //页面其他选项赋值
    UITextField *textField02 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:2 inSection:0];
    UITextField *textField03s = [self.tableView getCellSaveClass:[UITextField class] pathForRow:3 inSection:0];
    UITextField *textField03 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:4 inSection:0];
    UITextField *textField10 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:0 inSection:1];
    UITextField *textField11 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:1];
    UITextField *textField24 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:4 inSection:2];
    UITextField *textField30 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:0 inSection:3];
    UITextField *textField31 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:3];
    UITextField *textField32 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:2 inSection:3];
    
    NSString *GCBW = [copyOrderInfo objectFormatStringForKey:@"工程部位名称"];
    NSInteger GCBWId = [copyOrderInfo objectFormatIntegerForKey:@"工程部位ID"];
    NSString *SGBW = [copyOrderInfo objectFormatStringForKey:@"施工部位"];
    textField10.text = GCBW;
    textField11.text = [SGBW stringByReplacingOccurrencesOfString:GCBW withString:@""];
    self.projectPosition = @[@(GCBWId), GCBW];//设置工程部位信息
    
    UserLoginNSCoding *userInfo = [NSKeyedArchiverUserLogin getUserLoginData];
    textField30.text = userInfo.username;
    textField31.text = userInfo.Telephone;
    //textField30.text = [copyOrderInfo objectFormatStringForKey:@"工地联系人"];
    //textField31.text = [copyOrderInfo objectFormatStringForKey:@"工地联系电话"];
    
    textField02.text = [copyOrderInfo objectFormatStringForKey:@"施工班组"];
    textField03s.text = [copyOrderInfo objectFormatStringForKey:@"工地地址"];
    textField03.text = [copyOrderInfo objectFormatStringForKey:@"原材料要求"];
    textField32.text = [copyOrderInfo objectFormatStringForKey:@"备注"];
    textField24.text = [copyOrderInfo objectFormatFloatStringForKey:@"运输距离" retain:1];
    
    //修改订单
    if(self.orderID > 0){
        UITextField *textField04 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:5 inSection:0];
        UITextField *textField11 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:0 inSection:2];
        textField04.text = [copyOrderInfo objectFormatTimeStringForKey:@"计划开盘时间" inFormat:@"yyyy-MM-dd HH:mm:ss" outFormat:@"yyyy-MM-dd HH:mm"];
        textField11.text = [copyOrderInfo objectFormatFloatStringForKey:@"计划方量" retain:1];
    }
    
    //设置工程信息
    UITextField *textField01 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:0];
    NSString *projectName = [copyOrderInfo objectFormatStringForKey:@"工程名称"];
    NSString *projectID = [copyOrderInfo objectFormatStringForKey:@"工程ID"];
    textField01.text = projectName;
    
    if(projectName.length > 0 && projectID.length > 0){
        NSMutableDictionary *mDictionary = [NSMutableDictionary dictionary];
        [mDictionary setObject:projectName forKey:@"工程名称"];
        [mDictionary setObject:projectID forKey:@"ID"];
        _projectInfo = mDictionary;
    }
    
    //设置分发站点
    GEPickerTextField *textField05 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:6 inSection:0];
    NSString *stationName = [copyOrderInfo objectFormatStringForKey:@"原始分发站"];
    NSString *stationCode = [copyOrderInfo objectFormatStringForKey:@"分发站点编号"];
    textField05.text = stationName;
    
    if(stationName.length > 0 && stationCode.length > 0){
        NSMutableDictionary *stationDictionary = [NSMutableDictionary dictionary];
        [stationDictionary setObject:stationName forKey:@"U_Name"];
        [stationDictionary setObject:stationCode forKey:@"Code"];
        textField05.selectData = stationDictionary;
    }
    
    //设置强度等级
    GEPickerTextField *textField12 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:2];
    NSString *TQDName = [copyOrderInfo objectFormatStringForKey:@"砼强度"];
    NSString *TQDID = [copyOrderInfo objectFormatStringForKey:@"强度等级ID"];
    textField12.text = TQDName;
    
    if(TQDName.length > 0 && TQDID.length > 0){
        NSMutableDictionary *TQDDictionary = [NSMutableDictionary dictionary];
        [TQDDictionary setObject:TQDName forKey:@"StrengthLevel"];
        [TQDDictionary setObject:TQDID forKey:@"ID"];
        textField12.selectData = TQDDictionary;
    }
    
    //设置坍落度
    GEInputTableViewCell *TLDcell = [self.tableView getCellViewInRow:2 inSection:2];
    NSString *TLDString = [copyOrderInfo objectFormatStringForKey:@"坍落度"];
    NSArray *valueArray = [TLDString componentsSeparatedByString:@"±"];
    [TLDcell setMultiTextFieldValue:valueArray];
    
    //设置浇筑方式
    GEPickerTextField *textField14 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:3 inSection:2];
    NSString *JZFSName = [copyOrderInfo objectFormatStringForKey:@"浇筑方式"];
    NSString *JZFSID = [copyOrderInfo objectFormatStringForKey:@"浇筑方式ID"];
    textField14.text = JZFSName;
    
    if(JZFSName.length > 0 && JZFSID.length > 0){
        NSMutableDictionary *JZFSDictionary = [NSMutableDictionary dictionary];
        [JZFSDictionary setObject:JZFSName forKey:@"Definition"];
        [JZFSDictionary setObject:JZFSID forKey:@"Status"];
        textField14.selectData = JZFSDictionary;
    }
}

#pragma mark
#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0) return 7;
    if(section == 1) return 2;
    if(section == 2) return 5;
    if(section == 3) return 3;
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"CreateContractViewController%ld%ld",indexPath.section,indexPath.row];
    GEInputTableViewCell *cell = [self.tableView getCellViewInRow:indexPath.row inSection:indexPath.section];
    if (cell != nil) return cell;
    
    //合同基本信息
    if(indexPath.section == 0){
        if(indexPath.row == 0){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.titleName = @"合同编号";
            cell.placeholder = @"请选择合同编号";
            cell.isEdit = NO;
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 1){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.titleName = @"工程名称";
            cell.placeholder = @"请选择工程名称";
            cell.isMark = YES;
            cell.isEdit = NO;
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 2){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"施工班组";
            cell.placeholder = @"请输入施工班组";
            cell.isMark = YES;
            cell.isEdit = YES;
            //[cell setKeyboardType:UIKeyboardTypeDecimalPad filter:@"0123456789."];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 3){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"工地地址";
            cell.placeholder = @"请输入工地地址";
            cell.isMark = NO;
            cell.isEdit = YES;
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 4){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"原材要求";
            cell.placeholder = @"请输入原材要求";
            cell.isMark = NO;
            cell.isEdit = YES;
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 5){
            cell = [[GEInputTableViewCell alloc] initWithDateReuseIdentifier:CellIdentifier];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.titleName = @"开盘时间";
            cell.placeholder = @"请选择开盘时间";
            cell.isMark = YES;
            [cell setEnablePrevious:NO next:NO];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
            NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
            NSDateComponents *comps = [[NSDateComponents alloc] init];
            [comps setHour:1];
            NSDate *hourDate = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0];
            
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:00"];
            NSString *currentDateStr = [dateFormatter stringFromDate:hourDate];
            
            NSDate *nowDate = [dateFormatter dateFromString:currentDateStr];
            [cell setDatePickerMode:UIDatePickerModeDateAndTime defaultDate:nowDate];
            
        } else if(indexPath.row == 6){
            cell = [[GEInputTableViewCell alloc] initWithSelectReuseIdentifier:CellIdentifier request:nil];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.titleName = @"分发站点";
            cell.placeholder = @"请选择分发站点";
            cell.isMark = YES;
            [cell setEnablePrevious:NO next:NO];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        }
        return cell;
    }
    
    //工程部位，施工部位
    else if(indexPath.section == 1){
        if(indexPath.row == 0){
            cell = [[GEInputTableViewCell alloc] initWithSelectReuseIdentifier:CellIdentifier request:nil];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.titleName = @"工程部位";
            cell.placeholder = @"请选择工程部位";
            cell.isMark = YES;
            [cell setEnablePrevious:NO next:NO];
            //[cell addTarget:self action:@selector(textFieldDidChange:)];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 1){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"施工部位";
            cell.placeholder = @"请输入施工部位";
            cell.isMark = YES;
            cell.isEdit = YES;
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        }
    }
    
    //其他信息
    else if(indexPath.section == 2){
        if(indexPath.row == 0){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"计划方量";
            cell.placeholder = @"请输入计划方量";
            cell.isMark = YES;
            cell.isEdit = YES;
            [cell setKeyboardType:UIKeyboardTypeDecimalPad filter:@"0123456789." block:
             ^BOOL(UITextField *textField, NSRange range, NSString *string) {
                 //第一个不能是小数点
                 if (textField.text.length == 0 && [string hasPrefix:@"."]) return NO;
                 //字符不能包含多个“.”
                 NSArray *array = [textField.text componentsSeparatedByString:@"."];
                 if (array.count > 1) {
                     if ([string rangeOfString:@"."].location != NSNotFound) return NO;
                 }
                 //只允许输入小数点后一位
                 if ([textField.text rangeOfString:@"."].location != NSNotFound){
                     NSRange rangValue = [textField.text rangeOfString:@"."];
                     if (range.location - rangValue.location > 1) return NO;
                 }
                 return YES;
             }];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 1){
            cell = [[GEInputTableViewCell alloc] initWithSelectReuseIdentifier:CellIdentifier request:nil];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.titleName = @"强度等级";
            cell.placeholder = @"请选择强度等级";
            cell.isMark = YES;
            [cell setEnablePrevious:NO next:NO];
            //[cell addTarget:self action:@selector(textFieldDidChange:)];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 2){
            cell = [[GEInputTableViewCell alloc] initWithFieldMultiIdentifier:CellIdentifier type:@"[srk]±[srk]"];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"坍  落  度";
            cell.isMark = NO;
            cell.isEdit = NO;
            [cell setKeyboardType:UIKeyboardTypePhonePad filter:@"0123456789"];
            [cell setInputFiledLimitLength:3];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 3){
            WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
            [content setInterfaceType:InterfaceTypeGroup];
            [content setModuleType:@"HTGL_JCXX_LIST"];
            [content setResultBackType:ResultBackTypeCommonUnordered];
            [content addParameterKey:@"Type" value:@5];
            
            cell = [[GEInputTableViewCell alloc] initWithSelectReuseIdentifier:CellIdentifier request:content];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.titleName = @"浇筑方式";
            cell.placeholder = @"请选择浇筑方式";
            cell.isMark = YES;
            [cell setEnablePrevious:NO next:NO];
            //[cell addTarget:self action:@selector(textFieldDidChange:)];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 4){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"运输距离";
            cell.placeholder = @"请输入运输距离";
            cell.isMark = NO;
            cell.isEdit = YES;
            [cell setEnablePrevious:NO next:NO];
            [cell setKeyboardType:UIKeyboardTypeDecimalPad filter:@"0123456789." block:
             ^BOOL(UITextField *textField, NSRange range, NSString *string) {
                 //第一个不能是小数点
                 if (textField.text.length == 0 && [string hasPrefix:@"."]) return NO;
                 //字符不能包含多个“.”
                 NSArray *array = [textField.text componentsSeparatedByString:@"."];
                 if (array.count > 1) {
                     if ([string rangeOfString:@"."].location != NSNotFound) return NO;
                 }
                 //只允许输入小数点后一位
                 if ([textField.text rangeOfString:@"."].location != NSNotFound){
                     NSRange rangValue = [textField.text rangeOfString:@"."];
                     if (range.location - rangValue.location > 1) return NO;
                 }
                 return YES;
             }];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        }
        return cell;
    }
    
    //其他信息
    else if(indexPath.section == 3){
        if(indexPath.row == 0){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"现场技术员";
            cell.placeholder = @"请输入现场技术员";
            cell.isMark = NO;
            cell.isEdit = YES;
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 1){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"技术员电话";
            cell.placeholder = @"请输入技术员电话";
            cell.isMark = NO;
            cell.isEdit = YES;
            [cell setKeyboardType:UIKeyboardTypePhonePad filter:@"0123456789"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 2){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"备        注";
            cell.placeholder = @"可不填";
            cell.isMark = NO;
            cell.isEdit = YES;
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
        }
        return cell;
    }
    
    return nil;
}

#pragma mark
#pragma mark - UITableViewDelegate

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if(indexPath.section == 0 && indexPath.row == 0){ //合同编号
        OMOrderRealtimeDataViewController *viewController = [[OMOrderRealtimeDataViewController alloc] init];
        viewController.backResultDelagate = self;
        viewController.key = @"GetHTXX";
        
        AbstractNavigationController *navigationController = [[AbstractNavigationController alloc] initWithRootViewController:viewController];
        [self presentViewController:navigationController animated:YES completion:nil];
        
    } else if(indexPath.section == 0 && indexPath.row == 1){ //工程信息
        if(!self.allContractInfo){
            [self showAlertView:@"请先选择合同编号"]; return;
        }
        OMOrderProjectDataViewController *viewController = [[OMOrderProjectDataViewController alloc] initWithConcreteInfo:self.allContractInfo];
        viewController.backResultDelagate = self;
        viewController.key = @"GetGCXX";
        
        AbstractNavigationController *navigationController = [[AbstractNavigationController alloc] initWithRootViewController:viewController];
        [self presentViewController:navigationController animated:YES completion:nil];
        
    } else if(indexPath.section == 1 && indexPath.row == 0){ //工程部位
        CRECOMOrderProjectSiteController *viewController = [[CRECOMOrderProjectSiteController alloc] init];
        viewController.backResultDelagate = self;
        viewController.key = @"GetGCBW";
        
        AbstractNavigationController *navigationController = [[AbstractNavigationController alloc] initWithRootViewController:viewController];
        [self presentViewController:navigationController animated:YES completion:nil];
        return;
        
    } else if(indexPath.section == 2 && indexPath.row == 1){ //强度等级
        if(!self.allContractInfo){
            [self showAlertView:@"请先选择合同编号"]; return;
        }
        GEInputTableViewCell *geViewCell = (GEInputTableViewCell*)[self.tableView getCellViewInRow:1 inSection:2];
        NSArray *resultArray = geViewCell.objectData;
        if (resultArray) {
            NSDictionary *QDDJ = [NSDictionary dictionaryWithObject:@"%@" forKey:@"StrengthLevel"];
            [geViewCell setPickerTextFieldComponents:resultArray keyValue:@[QDDJ]];
        }
        
    } else if(indexPath.section == 2 && indexPath.row == 3){ //浇筑方式
        GEInputTableViewCell *geViewCell = (GEInputTableViewCell*)[self.tableView getCellViewInRow:3 inSection:2];
        NSArray *resultArray = geViewCell.objectData;
        if (resultArray) {
            NSDictionary *JZFS = [NSDictionary dictionaryWithObject:@"%@" forKey:@"Definition"];
            [geViewCell setPickerTextFieldComponents:resultArray keyValue:@[JZFS]];
        }
        
    } else if(indexPath.section == 0 && indexPath.row == 6){ //分发站点
        if(!self.allContractInfo){
            [self showAlertView:@"请先选择合同编号"]; return;
        }
        GEInputTableViewCell *geViewCell = (GEInputTableViewCell*)[self.tableView getCellViewInRow:6 inSection:0];
        NSArray *resultArray = geViewCell.objectData;
        if (resultArray) {
            NSDictionary *FFZD = [NSDictionary dictionaryWithObject:@"%@" forKey:@"U_Name"];
            [geViewCell setPickerTextFieldComponents:resultArray keyValue:@[FFZD]];
        }
    }
    
    //键盘事件处理
    UITextField *textField = [self.tableView getCellSaveClass:[UITextField class] pathForRow:indexPath.row inSection:indexPath.section];
    if (textField) [textField becomeFirstResponder];
    
}

#pragma mark - UIAlertView

- (void)showAlertView:(NSString*)message
{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"温馨提示" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    alert.alertViewStyle=UIAlertViewStyleDefault;
    [alert show];
}


#pragma mark - PresentResultDelagate
- (void) backResultDataKey:(id)key dataValue:(id)backValue
{
    //获取到合同全部信息
    if (key && [key isEqualToString:@"GetHTXX"]) {
        NSArray *results = backValue;
        _allContractInfo = results[0];
        [self setContractInfo:_allContractInfo];
        
        //添加合同工程信息（如果只有一个，就默认选中）
        _projectArray = results[1];
        if(self.projectArray.count == 1){
            _projectInfo = [self.projectArray firstObject];
            [self setProjectInfo:self.projectInfo];
        }
        
    } else if (key && [key isEqualToString:@"GetGCXX"]) {
        //选择某合同工程信息
        NSDictionary *results = backValue;
        _projectInfo = results;
        [self setProjectInfo:_projectInfo];
        
    } else if (key && [key isEqualToString:@"GetGCBW"]) {
        //选择某工程部位
        NSArray *results = backValue;
        _projectPosition = results;
        
        UITextField *textField = [self.tableView getCellSaveClass:[UITextField class] pathForRow:0 inSection:1];
        textField.text = results[1];
    }
    
    
}

- (void)setContractInfo:(NSDictionary*)itemContractInfo
{
    //清理工程信息
    UITextField *textField1 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:0];
    textField1.text = @"";
    
    //清理数据
    UITextField *textField0 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:0 inSection:0];
    UITextField *textField3 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:2 inSection:0];
    UITextField *textField4 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:3 inSection:0];
    textField0.text = [itemContractInfo objectFormatStringForKey:@"合同编号"];
    textField3.text = [itemContractInfo objectFormatStringForKey:@"施工班组"];
    textField4.text = [itemContractInfo objectFormatStringForKey:@"工地地址"];
    
    //设置合同计划方量
    UITextField *textField4s = [self.tableView getCellSaveClass:[UITextField class] pathForRow:0 inSection:2];
    textField4s.placeholder = [itemContractInfo objectFormatStringForKey:@"剩余签订量" format:@"该合同剩余%@方" defaults:@"请输入计划方量"];
    
    //合同ID
    NSString *contractID = [itemContractInfo objectFormatStringForKey:@"合同ID"];
    
    //获取分发站点
    WebServiceCommonRequest *contentFF = [[WebServiceCommonRequest alloc] init];
    [contentFF setInterfaceType:InterfaceTypeGroup];
    [contentFF setModuleType:@"DDGL_HTFFZD_LIST"];
    [contentFF setResultBackType:ResultBackTypeCommonUnordered];
    [contentFF addParameterKey:@"合同ID" value:contractID];
    
    GEInputTableViewCell *ZDFFcell = [self.tableView getCellViewInRow:6 inSection:0];
    [ZDFFcell setPickerTextFieldComponents:[NSArray array] keyValue:[NSArray array]];
    [ZDFFcell againGetWebServiceCommonRequest:contentFF];
    
    //重新获取强度等级数据
    WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
    [content setInterfaceType:InterfaceTypeGroup];
    [content setModuleType:@"DDGL_QDDJ_LIST"];
    [content setResultBackType:ResultBackTypeCommonUnordered];
    [content addParameterKey:@"合同ID" value:contractID];
    
    GEInputTableViewCell *QDDJcell = [self.tableView getCellViewInRow:1 inSection:2];
    [QDDJcell setPickerTextFieldComponents:[NSArray array] keyValue:[NSArray array]];
    [QDDJcell againGetWebServiceCommonRequest:content];
}

- (void)setProjectInfo:(NSDictionary*)itemContractInfo
{
    //清理数据
    UITextField *textField1 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:0];
    textField1.text = [itemContractInfo objectFormatStringForKey:@"工程名称"];
    
    //清理数据
    UITextField *textField2 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:4 inSection:2];
    textField2.text = [itemContractInfo objectFormatFloatStringForKey:@"运输距离" retain:1];
    
    //清理数据
    UserLoginNSCoding *userInfo = [NSKeyedArchiverUserLogin getUserLoginData];
    UITextField *textField20 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:0 inSection:3];
    UITextField *textField21 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:3];
    textField20.text = userInfo.username;
    textField21.text = userInfo.Telephone;
    //textField20.text = [itemContractInfo objectFormatStringForKey:@"工地联系人"];
    //textField21.text = [itemContractInfo objectFormatStringForKey:@"工地联系电话"];
}

@end
